package cc.autoapi.pucong.auto.flow.core.flow.execNode.task.system;


import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.node.ExecNode;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.node.ExecParamIn;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.exception.AutoDesignException;
import com.alibaba.fastjson2.JSONObject;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.node.NodeParamIn;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.node.NodeParamOut;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.task.AbstractTask;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.task.extend.AbstractScriptTask;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.task.extend.ExtendTaskInstance;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class JavaScriptTask extends AbstractTask {

    private List<NodeParamIn> paramsIns;
    private List<NodeParamOut> paramsOuts;

    public JavaScriptTask() {
        paramsIns = new ArrayList<>();

        paramsOuts = new ArrayList<>();
    }

    @Override
    public void run() {
        String content = getExecNode().getNodeScript();
        if (StringUtils.isBlank(content)) {
            throw new AutoDesignException("脚本内容不能为空！,节点名称：" + getExecNode().getNodeName());
        }
        // 执行脚本
        ExecNode execNode = getExecNode();
        List<ExecParamIn> paramInList = execNode.getParamInList();
        JSONObject jsonObjectParams = new JSONObject();
        for (ExecParamIn paramIn : paramInList) {
            if (paramIn.getParamCode().equals("javaScript")) {
                continue;
            }
            String paramCode = paramIn.getParamCode();
            Object dateValue1 = paramIn.getDateValue();
            jsonObjectParams.put(paramCode, dateValue1);
        }
        // 执行
        AbstractScriptTask task = ExtendTaskInstance.getTask(content);
        JSONObject result = task.handle(jsonObjectParams);
        if (result != null) {
            Set<String> strings = result.keySet();
            for (String string : strings) {
                setParamOutValue(string, result.get(string));
            }
        }
    }

    @Override
    public String getTaskName() {
        return "java脚本节点";
    }

    @Override
    public String getTaskDesc() {
        return " 本组件可以把脚本内容设置到“script”属性。脚本内容必须是java8语法，并继承cc.autoapi.pucong.auto.flow.auto.core.flow.execNode.task.extend.AbstractScriptTask类，实现handle方法，根据业务功能智能生成包名和类名。\n" +
                "                \n" +
                "                AbstractScriptTask类如下：\n" +
                "                    package cc.autoapi.pucong.auto.flow.auto.core.flow.execNode.task.extend;\n" +
                "                                   \n" +
                "                    import com.alibaba.fastjson2.JSONObject;\n" +
                "                                    \n" +
                "                    /**\n" +
                "                     * 扩展的自定义组件，需要基础此类\n" +
                "                     */\n" +
                "                    public abstract class AbstractScriptTask {\n" +
                "                        /**\n" +
                "                         * 执行\n" +
                "                         *\n" +
                "                         * @return\n" +
                "                         */\n" +
                "                        public abstract JSONObject handle(JSONObject json);\n" +
                "                    }\n" +
                "                \n" +
                "                生成示例：\n" +
                "                    package cc.autoapi.pucong.auto.flow.auto.core.flow.execNode.task.extend;\n" +
                "                                    \n" +
                "                    import com.alibaba.fastjson2.JSONObject;\n" +
                "                    import java.util.List;\n" +
                "                    import java.util.stream.Collectors;\n" +
                "                                    \n" +
                "                    public class DemoName extends AbstractScriptTask {\n" +
                "                        @Override\n" +
                "                        public JSONObject handle(JSONObject json) {\n" +
                "                            JSONObject result = new JSONObject();\n" +
                "                            // do something\n" +
                "                            return result;\n" +
                "                        }\n" +
                "                    }";
    }

    @Override
    public Boolean getTaskParamInAuto() {
        return true;
    }

    @Override
    public String getTaskSvgIcon() {
        return "<svg t=\"1728283628586\" class=\"icon\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"5486\" width=\"32\" height=\"32\"><path d=\"M725.952 170.048c-29.248 20.096-56.704 38.4-87.808 62.208-23.744 18.24-65.792 45.696-67.648 78.592-3.648 53.056 78.656 102.4 34.752 170.048-16.448 25.6-43.904 36.608-78.592 53.056-3.712-7.296 9.088-14.656 14.592-21.952 54.848-78.656-56.704-104.256-42.048-201.152 14.656-96.896 124.352-128 226.752-140.8z\" fill=\"#FF1515\" p-id=\"5487\"></path><path d=\"M563.2 0c16.448 16.448 29.248 47.552 29.248 78.656 0 96.896-102.4 151.744-151.744 215.744-11.008 14.656-25.6 36.544-25.6 60.352 0 52.992 54.848 111.552 74.944 153.6C457.152 486.4 415.104 455.296 384 420.48 354.752 384 323.648 327.296 351.104 276.096c40.192-74.944 162.688-120.64 206.592-201.152 11.008-20.096 20.096-51.2 5.504-74.944z\" fill=\"#FF1515\" p-id=\"5488\"></path><path d=\"M353.6 500.544c9.728-2.752 19.072-5.376 26.752-8.64a124.544 124.544 0 0 0-28.288 0.832c-4.672 0.512-8.768 0.96-11.968 0.96l-10.048 0.768c-50.56 3.84-150.72 11.328-149.056 52.288 0 36.544 93.312 45.696 133.504 49.344 120.704 7.36 296.256-3.648 352.896-45.696 9.152-5.504 25.6-16.448 21.952-23.744-89.6 16.448-219.392 25.6-325.44 20.096-25.6 0-53.056 0-71.36-14.656 12.8-17.92 38.208-25.088 61.056-31.552zM327.04 609.856c3.328-3.072 5.888-5.504 0.256-6.4-21.952 5.44-71.296 18.24-69.504 45.696 1.856 21.952 49.408 36.544 76.8 42.048 107.904 21.952 254.208 5.504 329.152-29.248-10.368-1.728-19.52-9.216-28.8-16.832-10.368-8.384-20.864-16.96-33.344-17.92-7.296-1.216-15.36 1.6-24.32 4.736a162.56 162.56 0 0 1-14.08 4.416c-65.856 12.8-199.296 25.6-241.408-16.448-1.728-3.584 2.112-7.104 5.248-10.048zM360.192 711.68c2.112-2.24 4.48-4.736 0-5.824l-10.752 2.816c-29.568 7.552-58.112 14.912-53.248 55.68 78.656 60.352 270.656 40.192 351.104-7.296-10.624-2.496-18.688-10.048-27.008-17.92-10.24-9.6-20.8-19.52-36.992-20.48-7.808-1.28-15.68 2.048-23.488 5.376-3.2 1.344-6.272 2.688-9.408 3.712-56.704 12.8-170.048 27.456-192-12.8-0.704-0.64 0.512-1.92 1.792-3.328z\" fill=\"#2365C4\" p-id=\"5489\"></path><path d=\"M264.064 783.36c9.728-0.576 18.56-1.088 24.832-2.56-42.048-36.544-177.344-20.096-179.2 36.544-1.792 32.96 40.256 56.704 75.008 67.712 107.84 36.544 279.744 38.4 418.752 23.744 64-7.296 221.248-34.752 213.888-104.256-3.648-18.24-20.096-31.04-38.4-32.896 14.656 64-102.4 84.096-164.544 91.456-135.296 14.592-298.048 10.944-378.496-20.16-14.656-5.44-36.608-20.096-34.752-32.896 2.688-22.848 36.032-24.96 62.912-26.624z\" fill=\"#2365C4\" p-id=\"5490\"></path><path d=\"M499.2 987.456c-93.248-11.008-182.848-23.808-257.856-56.704 197.504 47.552 486.4 43.904 625.408-56.704 1.984-1.472 4.032-3.392 6.016-5.376 5.44-5.12 11.136-10.56 17.728-9.216-30.912 92.8-140.224 108.8-245.184 124.032-12.8 1.92-25.6 3.776-38.208 5.76 0-1.792-107.904-1.792-107.904-1.792zM852.096 565.056c-1.792-75.008-89.6-91.456-140.8-47.552 40.256-9.152 75.008 9.152 82.304 36.544 11.968 58.304-42.048 101.952-79.168 131.904-8.32 6.72-15.744 12.672-21.376 18.048 69.44-3.648 162.688-53.056 159.04-138.944z\" fill=\"#2365C4\" p-id=\"5491\"></path></svg>";
    }

    @Override
    public List<NodeParamIn> getPropertyParamsIns() {
        return paramsIns;
    }

    @Override
    public List<NodeParamOut> getPropertyParamsOuts() {
        return paramsOuts;
    }
}
